package ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.SQLException;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import cas.SerialPortHandler;
import dao.DAOFactory;
import dao.oracle.JenisKeretaApiDao;
import dao.oracle.RuteDao;

public class EastPanel extends JPanel {

	private JComboBox portCombo;
	private JLabel rearLamp;
	private JLabel frontLamp;
	private JComboBox kaCombo;
	private JSpinner gerbongIsi;
	private JSpinner gerbongKosong;
	private JComboBox ruteCombo;
	private MainFrame mainFrame;
	private JSpinner kecepatanSpinner;

	/**
	 * Create the panel.
	 */
	public EastPanel(MainFrame frame) {
		super();
		this.mainFrame = frame;
		setBorder(null);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gbl_panel);

		JLabel lblPort = new JLabel("Port :");
		GridBagConstraints gbc_lblPort = new GridBagConstraints();
		gbc_lblPort.insets = new Insets(0, 0, 5, 5);
		gbc_lblPort.gridx = 0;
		gbc_lblPort.gridy = 0;
		add(lblPort, gbc_lblPort);
		SerialPortHandler serialPortHandler = new SerialPortHandler();
		String[] portNameList = serialPortHandler.getPortNameList();
		portCombo = new JComboBox();
		lblPort.setLabelFor(portCombo);
		for (int i = 0; i < portNameList.length; i++) {
			portCombo.insertItemAt(portNameList[i], i);
		}
		GridBagConstraints gbc_portCombo = new GridBagConstraints();
		gbc_portCombo.gridwidth = 2;
		gbc_portCombo.insets = new Insets(0, 5, 5, 0);
		gbc_portCombo.fill = GridBagConstraints.HORIZONTAL;
		gbc_portCombo.gridx = 0;
		gbc_portCombo.gridy = 1;
		add(portCombo, gbc_portCombo);

		rearLamp = new JLabel("");
		rearLamp.setIcon(getImageIcon("/icon/green.png"));
		GridBagConstraints gbc_rearLamp = new GridBagConstraints();
		gbc_rearLamp.insets = new Insets(0, 0, 5, 5);
		gbc_rearLamp.gridx = 0;
		gbc_rearLamp.gridy = 2;
		add(rearLamp, gbc_rearLamp);

		frontLamp = new JLabel("");
		frontLamp.setIcon(getImageIcon("/icon/green.png"));
		GridBagConstraints gbc_frontLamp = new GridBagConstraints();
		gbc_frontLamp.insets = new Insets(0, 0, 5, 0);
		gbc_frontLamp.gridx = 1;
		gbc_frontLamp.gridy = 2;
		add(frontLamp, gbc_frontLamp);

		JLabel lblKa = new JLabel("KA :");
		GridBagConstraints gbc_lblKa = new GridBagConstraints();
		gbc_lblKa.anchor = GridBagConstraints.WEST;
		gbc_lblKa.insets = new Insets(0, 5, 5, 5);
		gbc_lblKa.gridx = 0;
		gbc_lblKa.gridy = 3;
		add(lblKa, gbc_lblKa);

		kaCombo = new JComboBox();
		lblKa.setLabelFor(kaCombo);
		try {
			JenisKeretaApiDao jenisKaDao = DAOFactory.createJenisKeretaApiDao();
			List<String> listKABean = jenisKaDao.getListJenisKeretaApi();
			for(int i = 0; i < listKABean.size();i++){
				kaCombo.insertItemAt(listKABean.get(i), i);
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
		GridBagConstraints gbc_kaCombo = new GridBagConstraints();
		gbc_kaCombo.insets = new Insets(0, 5, 5, 0);
		gbc_kaCombo.gridwidth = 2;
		gbc_kaCombo.fill = GridBagConstraints.HORIZONTAL;
		gbc_kaCombo.gridx = 0;
		gbc_kaCombo.gridy = 4;
		add(kaCombo, gbc_kaCombo);

		JLabel lblJumlahGerbong = new JLabel("Jumlah Gerbong :");
		GridBagConstraints gbc_lblJumlahGerbong = new GridBagConstraints();
		gbc_lblJumlahGerbong.gridwidth = 2;
		gbc_lblJumlahGerbong.insets = new Insets(0, 5, 5, 0);
		gbc_lblJumlahGerbong.gridx = 0;
		gbc_lblJumlahGerbong.gridy = 5;
		add(lblJumlahGerbong, gbc_lblJumlahGerbong);

		gerbongIsi = new JSpinner();
		gerbongIsi.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				gerbongIsiStateChanged(arg0);
			}
		});
		gerbongIsi.setModel(new SpinnerListModel(new String[] {"Gerbong Isi", ""}));
		lblJumlahGerbong.setLabelFor(gerbongIsi);
		GridBagConstraints gbc_gerbongIsi = new GridBagConstraints();
		gbc_gerbongIsi.fill = GridBagConstraints.HORIZONTAL;
		gbc_gerbongIsi.gridwidth = 2;
		gbc_gerbongIsi.insets = new Insets(0, 5, 5, 0);
		gbc_gerbongIsi.gridx = 0;
		gbc_gerbongIsi.gridy = 6;
		add(gerbongIsi, gbc_gerbongIsi);

		gerbongKosong = new JSpinner();
		gerbongKosong.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				gerbongKosongStateChanged(arg0);				
			}
		});
		gerbongKosong.setModel(new SpinnerListModel(new String[] {"Gerbong Kosong",""}));
		GridBagConstraints gbc_gerbongKosong = new GridBagConstraints();
		gbc_gerbongKosong.fill = GridBagConstraints.HORIZONTAL;
		gbc_gerbongKosong.gridwidth = 2;
		gbc_gerbongKosong.insets = new Insets(0, 5, 5, 0);
		gbc_gerbongKosong.gridx = 0;
		gbc_gerbongKosong.gridy = 7;
		add(gerbongKosong, gbc_gerbongKosong);

		JLabel lblRute = new JLabel("Rute :");
		GridBagConstraints gbc_lblRute = new GridBagConstraints();
		gbc_lblRute.anchor = GridBagConstraints.WEST;
		gbc_lblRute.insets = new Insets(0, 5, 5, 5);
		gbc_lblRute.gridx = 0;
		gbc_lblRute.gridy = 8;
		add(lblRute, gbc_lblRute);

		try {
			RuteDao ruteDao = DAOFactory.createRuteDao();
			ruteCombo = new JComboBox(ruteDao.getListNamaRute());
			lblRute.setLabelFor(ruteCombo);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(this.mainFrame, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
		GridBagConstraints gbc_ruteCombo = new GridBagConstraints();
		gbc_ruteCombo.insets = new Insets(0, 5, 5, 0);
		gbc_ruteCombo.gridwidth = 2;
		gbc_ruteCombo.fill = GridBagConstraints.HORIZONTAL;
		gbc_ruteCombo.gridx = 0;
		gbc_ruteCombo.gridy = 9;
		add(ruteCombo, gbc_ruteCombo);

		JLabel lblKecepatan = new JLabel("Kecepatan :");
		GridBagConstraints gbc_lblKecepatan = new GridBagConstraints();
		gbc_lblKecepatan.insets = new Insets(0, 5, 5, 0);
		gbc_lblKecepatan.anchor = GridBagConstraints.WEST;
		gbc_lblKecepatan.gridwidth = 2;
		gbc_lblKecepatan.gridx = 0;
		gbc_lblKecepatan.gridy = 10;
		add(lblKecepatan, gbc_lblKecepatan);

		kecepatanSpinner = new JSpinner();
		kecepatanSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				if(mainFrame.getCas()!=null){
					mainFrame.getCas().getGpsSimulator().getSpeed().setDestinationSpeed(Double.valueOf(kecepatanSpinner.getValue().toString())*1000.0/3600.0);
				}
			}
		});
		lblKecepatan.setLabelFor(kecepatanSpinner);
		GridBagConstraints gbc_kecepatanSpinner = new GridBagConstraints();
		gbc_kecepatanSpinner.fill = GridBagConstraints.HORIZONTAL;
		gbc_kecepatanSpinner.gridwidth = 2;
		gbc_kecepatanSpinner.insets = new Insets(0, 5, 0, 0);
		gbc_kecepatanSpinner.gridx = 0;
		gbc_kecepatanSpinner.gridy = 11;
		add(kecepatanSpinner, gbc_kecepatanSpinner);

	}
	private void gerbongIsiStateChanged(ChangeEvent arg0) {
		if(gerbongIsi.getModel()instanceof SpinnerListModel){
			gerbongIsi.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));	
		}
	}
	private void gerbongKosongStateChanged(ChangeEvent evt) {
		if(gerbongKosong.getModel()instanceof SpinnerListModel){
			gerbongKosong.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));	
		}		
	}
	private ImageIcon getImageIcon(String name){
		return new ImageIcon(EastPanel.class.getResource(name));
	}
	public JSpinner getKecepatanSpinner() {
		return kecepatanSpinner;
	}
	public JComboBox getPortCombo() {
		return portCombo;
	}
	public JComboBox getKaCombo() {
		return kaCombo;
	}
	public JSpinner getGerbongIsi() {
		return gerbongIsi;
	}
	public JSpinner getGerbongKosong() {
		return gerbongKosong;
	}
	public JLabel getRearLamp() {
		return rearLamp;
	}
	public JLabel getFrontLamp() {
		return frontLamp;
	}
	public JComboBox getRuteCombo() {
		return ruteCombo;
	}
	
}
